package com.yilin.tms.system.controller;

import com.yilin.commons.encrypt.Md5Util;
import com.yilin.commons.exception.BusinessException;
import com.yilin.tms.core.application.AppUtil;
import com.yilin.tms.core.application.PageData;
import com.yilin.tms.core.application.ReturnData;
import com.yilin.tms.core.commons.base.BaseController;
import com.yilin.tms.core.commons.utils.ParamUtil;
import com.yilin.tms.core.entity.system.applets.H5Applets;
import com.yilin.tms.system.service.IH5AppletsService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/system/applets/")
public class H5AppletsController extends BaseController {
    @Resource
    private IH5AppletsService appletsService;

    @PostMapping("getAppletsPage")
    public ReturnData<PageData<H5Applets>> getAppletsPage(H5Applets query, Integer page, Integer limit) {
        PageData<H5Applets> pageData= appletsService.getAppletsPage(query,page,limit);
        return jsonView(SUCCESS_CODE, "获取成功", pageData);
    }

    @PostMapping("saveUpdateApplets")
    public ReturnData<Object> saveUpdateApplets(H5Applets h5Applets) {
        ParamUtil.isNull(h5Applets.getName(),"请输入名称");
        ParamUtil.isNull(h5Applets.getLinkUrl(),"请输入地址");
        ParamUtil.isNull(h5Applets.getOperator(),"请输入运营方");
        ParamUtil.isNull(h5Applets.getVersionNo(),"请输入版本号");
        ParamUtil.isNull(h5Applets.getIconUrl(),"请传入图标");
        ParamUtil.isNull(h5Applets.getWhetherStatusBar(),"请选择是否显示状态栏");
        ParamUtil.isNull(h5Applets.getWhetherTitleBar(),"请选择是否显示标题栏");
        if(h5Applets.getWhetherTitleBar()){
            ParamUtil.isNull(h5Applets.getTitleBarBgColor(),"请选择背景颜色");
            ParamUtil.isNull(h5Applets.getTitleBarFrontColor(),"请选择背景颜色前景");
        }
        if(h5Applets.getId()!=null){
            H5Applets h5AppletsById = appletsService.getAppletsById(h5Applets.getId());
            h5Applets.setAppId(h5AppletsById.getAppId());
        }else if(h5Applets.getAppId()==null){
            String  appid= Md5Util.md5By16(AppUtil.getUUID());
            h5Applets.setAppId(appid);
            h5Applets.setAppletStatus(H5Applets.AppletStatus.save);
        }
        H5Applets h5Applets1 =appletsService.getAppletsByAppId(h5Applets.getAppId());
        if(h5Applets1 !=null){
            if(h5Applets.getId()==null)throw  new BusinessException("该APPID已经存在");
            if(!h5Applets.getId().equals(h5Applets1.getId()))throw  new BusinessException("该APPID已经存在");
        }
        appletsService.saveOrUpdates(h5Applets);
        return jsonView(SUCCESS_CODE, "操作成功");
    }

    @PostMapping("deleteApplets")
    public ReturnData<Object> deleteApplets(String appletsId) {
        ParamUtil.isNull(appletsId,"请传入信息");
        appletsService.deleteApplets(appletsId);
        return jsonView(SUCCESS_CODE, "操作成功");
    }
    @PostMapping("getAppletsByAppId")
    public ReturnData<Object> getAppletsByAppId(String appId) {
       H5Applets h5Applets = appletsService.getAppletsByAppId(appId);
        return jsonView(SUCCESS_CODE, "查询成功", h5Applets);
    }


}
